<?php
	require("libraries/student.php");
	class Cateblog extends Student{
		public function __construct(){
			parent::__construct();
			$this->load->model("model_cateblog");
			$this->load->helper("form");
			$this->load->library("form_validation");
			$this->load->library("string");
		}
		public function index(){
			$config['base_url'] = base_url().'admin/cateblog/index/';
			$config['total_rows'] = $this->model_cateblog->count_all();
			$data['title'] = "Manage category blogs";
			$data['template'] = "cateblog/list_cate";
			$config['per_page'] = 10;
			$config['uri_segment'] = 4;
			$config['next_link'] = "Next";
			$config['prev_link'] = "Prev";
			$this->load->library("pagination");
			$this->pagination->initialize($config); 
			$start = $this->uri->segment(4);
			$data['data'] = "";
			$data['act'] = 4;
			$data['listcate']= $this->model_cateblog->listall($config['per_page'],$start);
			$this->load->view("layout",$data);
		}
		public function active(){
			$id   	= $this->uri->segment(4);
			$status	= $this->uri->segment(5);
			$db = array(
				"category_status" => $status 
			);
			$this->model_cateblog->update_status($db,$id);
			redirect(base_url()."admin/cateblog/");
		}
		public function add(){
		  	$data['title']= "Add category";
			$data['template'] = "cateblog/add_cate";
			$data['data'] = "";
			$data['act'] = 4;
			if($this->input->post("ok") != ""){
				$url = str_replace(' ', '-',strtolower($this->string->replace($this->input->post('cateblog_title'))));
				$value = array(
						"cateblog_title" => $this->input->post("cateblog_title"),
						"cateblog_rewrite" => $url,
						"cateblog_info" => $this->input->post("cateblog_info"),
						"cateblog_keys" => $this->input->post("cateblog_keys"),
						"cateblog_des" => $this->input->post("cateblog_des"),
						"cateblog_order" => $this->input->post("cateblog_order"),
						"destina_id" => $this->input->post("destina_id")
				);
				$this->model_cateblog->add($value);
				redirect(base_url()."admin/cateblog");
			}else{
				$this->load->view("layout",$data);
			}
		}
		public function update(){
			$id = $this->uri->segment(4);
			$data['title']= "Edit category";
			$data['template'] = "cateblog/edit_cate";
			$data['data'] = "";
			$data['act'] = 4;
			$data['getdata'] = $this->model_cateblog->getdata($id);
			if($this->input->post("ok") != ""){
				$url = str_replace(' ', '-',strtolower($this->string->replace($this->input->post('cateblog_title'))));
				$value = array(
						"cateblog_title" => $this->input->post("cateblog_title"),
						"cateblog_rewrite" => $url,
						"cateblog_info" => $this->input->post("cateblog_info"),
						"cateblog_keys" => $this->input->post("cateblog_keys"),
						"cateblog_des" => $this->input->post("cateblog_des"),
						"cateblog_order" => $this->input->post("cateblog_order"),
						"destina_id" => $this->input->post("destina_id")
				);
				$this->model_cateblog->update($value,$id);
				redirect(base_url()."admin/cateblog/index");
			}else{
				$this->load->view("layout",$data);
			}
		}
		public function del(){
			$id = $this->uri->segment(4);
			$this->model_cateblog->del($id);
			redirect(base_url()."admin/cateblog/index");
		}
		public function createThumbnail($fileName){
			$this->load->library('image_lib');
			//$this->load->helper('thumbnail_helper');
			$config['image_library'] = 'gd2';
			$config['source_image'] = 'uploads/cateblog/'.$fileName;
			$config['new_image'] = 'uploads/cateblog/thumb/'.$fileName;
			$config['create_thumb'] = TRUE;
			$config['maintain_ratio'] = TRUE;
			$config['thumb_marker'] = FALSE;
			$config['width'] = 160;
			$config['height'] = 130;
			$this->image_lib->initialize($config); 
			$this->image_lib->resize();
			$this->image_lib->clear();
		}
	}